<?php

namespace Utuo;

class Form {
	
	/**
	 * 获取表单选择列表
	 *
	 * @param array $options
	 * @param string $default
	 * @param string $firstOption
	 * @return string
	 */
	public static function getOption($options, $default = null, $firstOption = null) {
		empty($options) && $options = array();
		$ret = array();
		empty($firstOption) or $ret[] = "<option value=''>$firstOption</option>";
		
		foreach ($options as $k => $v) {
			
			$selected = !is_null($default) && $default !== '' && $default == $k ? " selected='selected'" : '';
			$ret[] = "<option value='$k'$selected>$v</option>";
		}
		
		return implode("\n", $ret);
	}
	
	/**
	 * 获取单选表单
	 *
	 * @param string $name
	 * @param array $options
	 * @param string $default
	 * @return string
	 */
	public static function getRadio($name, $options, $default = null) {
		$ret = array();
		foreach ($options as $k => $v) {
			$checked = !is_null($default) && $default == $k ? " checked='checked'" : '';
			$ret[] = "<label><input type='radio' name='$name' id='{$name}_$k' value='$k'$checked> $v</label>";
		}
		return implode("\n", $ret);
	}
	
	/**
	 * 获取多选表单
	 *
	 * @param string $name
	 * @param array $options
	 * @param string $default
	 * @return string
	 */
	public static function getCheckBox($name, $options, $default = array()) {
		$default == null and $default = array();
		!is_array($default) and $default = array(
				$default 
		);
		$ret = array();
		$i = 0;
		foreach ($options as $k => $v) {
			$checked = !is_null($default) && in_array($k, $default) ? " checked='checked'" : '';
			$subject = "<label><input type='checkbox' name='$name' id='{$name}_$k' value='$k'$checked> $v</label>";
			$ret[] = str_replace("{:index}", $i, $subject);
			$i++;
		}
		return implode("\n", $ret);
	}
	public static  function makeCheckbox($array,$check,$name){
		$checkbox="";
		foreach ($array as $k=>$v)
		{
			$checked=in_array($k, $check)?"checked":'';
			$checkbox.="<input type='checkbox' name='".$name."' value='".$k."' $checked/>".$v."&nbsp;&nbsp;";
		}
		return $checkbox;
	}
}